/**
 *@Description 物流公司配置
 *@Author chenxq
 *@Date 201516-01-08
 */
var storageCtrl  = new XFApp.client();
XFApp.client.prototype.storage = {
    successCode : "10000",
    page : null,
    urlItems : {
        queryByPageURL : basePath+"/logistics/storage/queryPage",
        getStorageURL : basePath+"/logistics/storage/detail",
        saveStorageURL : basePath+"/logistics/storage/save",
        delStorageURL : basePath+"/logistics/storage/delete"
    },
    queryPage : function(){
        _this = this;
        if(!!_this.page){
            _this.page.page("remote",0,$('#searchForm').serialize());
        }else{
            _this.page = $("#page").page({
                pageSize : 10,
                currentPageIndex : Number($("#currentPageIndex").val())-1,
                remote: {
                    url: _this.urlItems.queryByPageURL,
                    params: $('#searchForm').serialize(),
                    success: function (data, pageIndex) {
                        $("#currentPageIndex").val(data.result.pageNo);
                        if(data.code==_this.successCode){
                            var pageResult = data.result;
                            if(null == data
                                || null == data.result
                                || null == data.result.itemList
                                || data.result.itemList.length < 1){
                                $("#page_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
                            }else{
                                for(var index in pageResult.itemList){
                                    var element = pageResult.itemList[index];
                                    element["seqNum"]=parseInt(pageResult.pageSize)*(parseInt(pageResult.pageNo)-1)+parseInt(index)+1;
                                }
                                $("#page_grid").html($("#dataTmp").render(pageResult.itemList));
                            }
                        }else{
                            webDialogUtil.simpleAlert("数据查询失败!");
                        }
                    },
                    error : function(){
                        webDialogUtil.simpleAlert("数据查询失败!");
                    }
                },
                firstBtnText : '首页',
                lastBtnText : '尾页',
                prevBtnText : '上一页',
                nextBtnText : '下一页',
                showInfo : true,
                showJump : true,
                jumpBtnText : '跳转',
                showPageSizes : true,
                infoFormat : '共{pageCount}页,{total}项'
            });
        }
    },
    formValidate : function(){
        $("#editStorageForm").validate({
            rules: {
                storageName: "required",
                address: "required",
                storageFee: {
                    required:true,
                    number:true
                },
                inOutFee: {
                    required: true,
                    number:true
                },
                goldRebate: {
                    number:true
                },
                silverRebate: {
                    number:true
                }
            },
            messages: {
                storageName: "请填写服务名称",
                address: "请填写详细地址",
                storageFee: {
                    required:"请填写仓储费",
                    number:"仓储费必须为数字"
                },
                inOutFee: {
                    required: "请填写出入库操作费",
                    number:"出入库操作费必须为数字"
                },
                goldRebate: {
                    number:"金卡折扣必须为数字"
                },
                silverRebate: {
                    number:"银卡折扣必须为数字"
                }
            }
        });
        $("#editStorageForm").validate();
    },
    editStorageView : function(id,typeId){
        _this = this;
        if(null == id){
            return this.editStorageDialog(null,typeId);
        }
        $.ajax({
            url:_this.urlItems.getStorageURL,
            data:{id:id},
            type:'post',
            cache:false,
            dataType:'json',
            success:function(data) {
                if(false == data.isSuccess){
                    window.webDialogUtil.alert("提示","获取数据失败");
                    return;
                }
                _this.editStorageDialog(data.result,typeId);
            },
            error : function() {
                window.webDialogUtil.alert("提示","操作失败");
            }
        });
    },
    editStorageDialog : function(data,typeId){
        if(null != data){
            if(null != data.area){
                var area = data.area.split(" ");
                var prov = area[0];
                var city = area[1];
                var dist = area[2];
                $("#storageArea").citySelect({
                    prov: prov,
                    city: city,
                    dist: dist
                });
            }
            $("#editStorageForm #id").val(data.id);
            $("#editStorageForm #storageName").val(data.storageName);
            $("#editStorageForm #storageCty").val(data.storageCty);
            $("#editStorageForm #address").val(data.address);
            $("#editStorageForm #storageFee").val(data.storageFee);
            $("#editStorageForm #inOutFee").val(data.inOutFee);
            $("#editStorageForm #goldRebate").val((data.goldRebate*10).toFixed(2));
            $("#editStorageForm #silverRebate").val((data.silverRebate*10).toFixed(2));
            $("#editStorageForm #updateTime").text(data.updateTime);
            $("#editStorageForm #updatePerson").text(data.updatePerson);
        }else{
            $("#editStorageForm #id").val("");
            $("#editStorageForm #storageName").val("");
            $("#editStorageForm #storageCty").val("");
            $("#editStorageForm #address").val("");
            $("#editStorageForm #storageFee").val("");
            $("#editStorageForm #inOutFee").val("");
            $("#editStorageForm #goldRebate").val("");
            $("#editStorageForm #silverRebate").val("");
            $("#editStorageForm #updateTime").val("");
            $("#editStorageForm #updatePerson").val("");
        }
        webAlert({
            title:storageCtrl.storage.titleDisplay(typeId),
            content:document.getElementById("editView"),
            button:[
                {
                    name:"保存",
                    callback:function(){
                        var prov = $("#storageArea .prov").find("option:selected").text();
                        var city = $("#storageArea .city").find("option:selected").text();
                        var dist = $("#storageArea .dist").find("option:selected").text();
                        var storageArea = prov+ " " + city + " " + dist;
                        if(prov == '请选择' || city == '请选择'|| storageArea == null){
                            $("#area-error").text("请选择仓库区域");
                            return false;
                        }
                        $("#area").val(storageArea);
                        if($("#editStorageForm").validate().form()){
                            storageCtrl.storage.saveStorageForm();
                        }else{
                            return false;
                        }
                    }},
                {
                    name:"关闭",
                    callback:function(){
                        this.close();
                    }
                }]
        });
    },
    saveStorageForm : function(){
        _this = this;
        $.ajax( {
            url:_this.urlItems.saveStorageURL,
            data:$('#editStorageForm').serialize(),
            type:'post',
            cache:false,
            dataType:'json',
            success:function(data) {
                window.webDialogUtil.alert("提示",data.msg);
                storageCtrl.storage.queryPage();
            },
            error : function() {
                window.webDialogUtil.alert("提示","操作失败");
            }
        });
    },
    titleDisplay : function(typeId){
        switch(typeId){
            case 0:return "查看";
            case 1:return "修改";
            case 2:return "新增";
            default : return "";
        }
    },
    delStorageDialog : function(id){
        _this = this;
        webDialogUtil.simpleConfrimAlert("确定要删除此运单记录吗?", function(){storageCtrl.storage.delStorage(id);});
    },
    delStorage : function(id){
        _this = this;
        $.ajax({
            url:_this.urlItems.delStorageURL,
            data:{id:id},
            type:'post',
            cache:false,
            dataType:'json',
            success:function(data) {
                if(true == data.isSuccess){
                    webDialogUtil.simpleAlert(data.msg);
                    storageCtrl.storage.queryPage();
                }
            },
            error : function() {
                webDialogUtil.simpleAlert("操作失败!");
            }
        });
    },
    event : function(){
        _this = this;
        $("#searchButton").on("click",function(){
            storageCtrl.storage.queryPage();
        });
        storageCtrl.storage.queryPage();
        this.formValidate();
    }
};
$.views.converters({

});
$(function(){
    storageCtrl.storage.event();
});